package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.framework.enums.RedisCachePrefixEnum;
import com.lcf.framework.enums.RedisLockPrefixEnum;
import com.lcf.framework.exception.SysException;
import com.lcf.framework.utils.RedisCacheUtils;
import com.lcf.framework.utils.RedisLockUtils;
import com.lcf.framework.utils.UserUtils;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.SysTenantUserDao;
import com.lcf.system.entity.SysTenantUser;
import com.lcf.system.entity.SysUser;
import com.lcf.system.manager.SysTenantManager;
import com.lcf.system.query.user.SysTenantUserPageQuery;
import com.lcf.system.service.SysTenantUserService;
import com.lcf.system.vo.tenantUser.SysTenantUserPageVO;
import com.lcf.system.vo.tenantUser.SysTenantUserVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 租户-用户(SysTenantUser)表服务实现类
 *
 * @author lcf
 * @since 2025-03-02 23:00:39
 */
@Service("sysTenantUserService")
public class SysTenantUserServiceImpl extends ServiceImpl<SysTenantUserDao, SysTenantUser> implements SysTenantUserService {
    @Resource
    private RedisCacheUtils redisCacheUtils;
    @Resource
    private SysTenantManager sysTenantManager;
    @Resource
    private RedisLockUtils redisLockUtils;

    @Override
    public void add(String mobile) {
        SysUser sysUser = Db.lambdaQuery(SysUser.class).eq(SysUser::getMobile, mobile).one();
        if (sysUser == null) {
            throw new SysException("用户不存在");
        }

        redisLockUtils.tryLock(RedisLockPrefixEnum.ADD_USER_TO_TENANT, sysUser.getId(), () -> {
            boolean exists = Db.lambdaQuery(SysTenantUser.class)
                    .eq(SysTenantUser::getTenantId, UserUtils.getTenantId())
                    .eq(SysTenantUser::getUserId, sysUser.getId())
                    .exists();
            if (exists) {
                throw new SysException("用户已加入社区");
            }

            new SysTenantUser(UserUtils.getTenantId(), sysUser.getId()).insert();
        });
    }

    @Override
    public void quit() {
        long userId = UserUtils.getUserId();
        sysTenantManager.quit(UserUtils.getTenantId(), userId);

        //删除用户角色缓存
        long tenantId = UserUtils.getTenantId();
        redisCacheUtils.del(RedisCachePrefixEnum.USER_ROLE.getKey(userId + ":" + tenantId));
    }

    @Override
    public void kickOut(long userId) {
        sysTenantManager.quit(UserUtils.getTenantId(), userId);

        //删除用户角色缓存
        long tenantId = UserUtils.getTenantId();
        redisCacheUtils.del(RedisCachePrefixEnum.USER_ROLE.getKey(userId + ":" + tenantId));
    }

    @Override
    public PageVO<SysTenantUserPageVO> page(SysTenantUserPageQuery query) {
        Page<SysTenantUser> page = Db.lambdaQuery(SysTenantUser.class)
                .eq(SysTenantUser::getTenantId, UserUtils.getTenantId())
                .eq(query.getUserId() != null, SysTenantUser::getUserId, query.getUserId())
                .page(query.toPage());

        return new PageVO<>(page, SysTenantUserPageVO.class);
    }

    @Override
    public SysTenantUserVO getDetail(long userId) {
        SysUser sysUser = Db.lambdaQuery(SysUser.class).eq(SysUser::getId, userId).one();
        if (sysUser == null) {
            throw new SysException("用户不存在");
        }

        return BeanUtil.copyProperties(sysUser, SysTenantUserVO.class);
    }
}

